package logic

import (
	"context"

	"mail/service/receipt/api/internal/svc"
	"mail/service/receipt/api/internal/types"
	"mail/service/receipt/rpc/receiptclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type ConvergeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewConvergeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ConvergeLogic {
	return &ConvergeLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ConvergeLogic) Converge(req *types.ConvergeRequest) (resp []*types.ConvergeResponse, err error) {
	// todo: add your logic here and delete this line
	reply, err := l.svcCtx.ReceiptRpc.Converge(l.ctx, &receiptclient.ConvergeRequest{
		Uid: req.Uid,
	})
	if err != nil {
		return nil, err
	}

	deliberate := make([]*types.ConvergeResponse, 0, 10)
	for _, each := range reply.Data {
		deliberate = append(deliberate, &types.ConvergeResponse{
			Id:     each.Id,
			Uid:    each.Uid,
			Pid:    each.Pid,
			Amount: each.Amount,
			Status: each.Status,
		})
	}
	return deliberate, nil
}
